<?php


declare (strict_types=1);

namespace app\bbxw\controller\api;


use app\bbxw\service\PaymentService;

use think\admin\extend\CodeExtend;
use app\bbxw\controller\api\Auth;
use think\admin\Controller;

/**
 * 订单数据
 * @class Test
 * @package app\wechat\controller\api
 */
class Order extends Auth
{
   // protected $user_id = 1;
    public function getList()
    {
        $status_names = ["待支付","已支付","取消","退款中","已退款"];
        $list = $this->app->db->name('bbxw_order')->where('pay_user',$this->uuid)->order('id desc')->select()->toArray();
        foreach ($list as &$vo){
            $vo['package_info'] = json_decode($vo['package_info'],true);
            $vo['package_info']['duration'] = ceil($vo['package_info']['duration']/60);
            $vo['status_name'] = $status_names[$vo['pay_status']];
            if($vo['is_use']==1){
                $vo['status_name'] = "已使用";
            }
        }
        $this->success('获取成功',$list);
    }
    public function getDetail()
    {
        $id = $this->request->get('id');
        $detail = $this->app->db->name('bbxw_order')->where(['id'=>$id,'pay_user'=>$this->uuid])->find();
        $this->success('获取成功',$detail);
    }

    public function createOrder(){
        $data = $this->request->get();
        $this->_vali([
            'package_id.require'  => '请输入选择套餐'
        ],$data);
        $data['pay_user'] = $this->uuid;
        $order_no = CodeExtend::uniqidDate(18, 'NO');
        $package = $this->app->db->name('bbxw_package')->where('id',$data['package_id'])->find();
        if(empty($package)){
            $this->error('套餐不存在');
        }
        $data['order_no'] = $order_no;
        $data['amount'] = $package['price'];
        $data['package_info'] = json_encode($package);
        $this->app->db->name('bbxw_order')->insert($data);
        $s = PaymentService::create($this->user['openid'],$order_no,$package['name'],"wechat_xcx",$data['amount']);
        if($s['code']==1){
            $this->success('下单成功',['data'=>$s['data'],'params'=>$s['params']]);
        }else{
            $this->error("支付创建失败");
        }

    }

    public function updataOrder(){
        $data = $this->request->get();
        $this->_vali([
            'order_no.require'  => '请输入订单号'
        ],$data);
        $order = $this->app->db->name('bbxw_order')->where('order_no',$data['order_no'])->find();
        if(empty($order)){
            $this->error('订单不存在');
        }
        $result = PaymentService::query($data['order_no']);
        if (isset($result['trade_state']) && $result['trade_state'] === 'SUCCESS') {
            $this->app->db->name('bbxw_order')->where('id',$order['id'])->update(['pay_status'=>1,'pay_at'=>date('Y-m-d H:i:s')]);
            $this->success('支付成功',$order['id']);
        }else{
            $this->error('支付失败');
        }
    }

    public function begin(){
        $data = $this->request->get();
        $this->_vali([
            'id.require'  => '请输入订单号'
        ],$data);
        $order = $this->app->db->name('bbxw_order')->where('id',$data['id'])->find();
        if(empty($order)){
            $this->error('订单不存在');
        }
        $this->app->db->name('bbxw_order')->where('id',$data['id'])->update(['is_use'=>1]);
        $this->success('操作成功');
    }
}
